package dazuoye.基于主程序子程序风格;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * KWIC
 * 基于主程序/子程序风格
 * @author admin
 * @version 2023/11/01 10:33
 **/
public class KWIC1 {
    public static final int i=1;
    public static void main(String[] args) throws IOException {
        //主程序,通过调用子程序实现
        File file= new File("C:\\Users\\topto\\Desktop\\新建 Text Document.txt");
        BufferedReader input= new BufferedReader(new FileReader(file));
        String s;
        List<String> res=new ArrayList<>();
        while((s=input.readLine())!=null)
        {
            res=circularShifts(s,res,i);////////循环移位
        }

        orderOutput(res);//排序输出

    }
    public static List<String> circularShifts(String s,List<String> list,int n)
    {
        //A third way for styles to
        StringBuilder sb=new StringBuilder(s+" ");
        int i=0;
        int j=0;//双指针
        if(n!=0){
            for(char c:s.toCharArray())//从第n个单词开始
            {
                if(c==' '){
                    n--;
                }
                j++;
                if(n==0) break;
            }
            i=j;
        }

        int num=0;
        while(j<s.length())
        {

            while(j<s.length()&&s.charAt(j)!=' '){
                j++;
            }
            String pre=s.substring(i,j+1>=s.length()?s.length():j+1);//获取前置字符串
            sb.delete(i-num,j+1-num);
            sb.append(pre);
            list.add(sb.toString());
            i=++j;
            num+=pre.length();//记录已移位的字符串数量
        }
        return list;
    }
    public static void orderOutput(List<String> list)
    {
        Collections.sort(list);
        for(String s:list){
            System.out.println(s);
        }
    }
}
